import 'dart:math';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:speed_random/pages/home/rand_com/logic.dart';
import 'package:speed_random/pages/home/rand_com/state.dart';


class ViewBodyToss2 extends StatefulWidget {
  final RandComLogic logic;
  final RandComState state;

  const ViewBodyToss2({Key? key, required this.logic, required this.state})
      : super(key: key);

  @override
  State<ViewBodyToss2> createState() => _ViewBodyTossState();
}

class _ViewBodyTossState extends State<ViewBodyToss2>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _rotationYAnimation;
  late Animation<double> _rotationXAnimation;
  late Animation<double> _positionYAnimation;
  late Animation<double> _scaleAnimation;

  final Random _random = Random();
  bool _isFront = true;
  bool _isAnimating = false;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 1500),
      vsync: this,
    );

    // 设置动画曲线组合
    _rotationYAnimation = Tween(begin: 0.0, end: 10.0 * pi).animate(
      CurvedAnimation(
        parent: _controller,
        curve: const Interval(0.0, 0.8, curve: Curves.easeInOut),
      ),
    );

    _rotationXAnimation = Tween(begin: 0.0, end: 0.2 * pi).animate(
      CurvedAnimation(
        parent: _controller,
        curve: const Interval(0.0, 0.6, curve: Curves.easeOut),
      ),
    );

    _positionYAnimation = Tween(begin: 0.0, end: -150.0).animate(
      CurvedAnimation(
        parent: _controller,
        curve: const Interval(0.0, 0.5, curve: Curves.easeOut),
      ),
    );

    _scaleAnimation = Tween(begin: 1.0, end: 1.2).animate(
      CurvedAnimation(
        parent: _controller,
        curve: const Interval(0.0, 0.5, curve: Curves.easeOut),
      ),
    );

    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {

          widget.logic.generateRandomNumbersStop(2,funStop:(){
            setState(()  {
            _isFront = widget.state.randomNumbers.first=="正面"; // 随机决定正反面

            widget.logic.payRunMp3(type: "stop");

            _isAnimating = false;
          });


        });
        _controller.reset();
      }
    });
  }

  void _flipCoin() {
    if (_isAnimating) return;
    setState(() => _isAnimating = true);
    _controller.forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    widget.state.funStart=() {
      widget.logic.generateRandomNumbersRunOne(isSaveCache: true);
      widget.state.isRun.value = 1;
      widget.logic.payRunMp3();
      _flipCoin();
    };
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        AnimatedBuilder(
          animation: _controller,
          builder: (context, child) {
            return Transform(
              alignment: Alignment.center,
              transform: Matrix4.identity()
                ..setEntry(3, 2, 0.001)
                ..rotateY(_rotationYAnimation.value)
                ..rotateX(_rotationXAnimation.value),
              child: Transform.translate(
                offset: Offset(0, _positionYAnimation.value),
                child: Transform.scale(
                  scale: _scaleAnimation.value,
                  child: Container(
                    width: 150,
                    height: 150,
                    child: _isFront
                        ? Image.asset('assets/images/icons/home_toss.png')
                        : Image.asset('assets/images/icons/home_tossn.png'),
                  ),
                ),
              ),
            );
          },
        ),
        const SizedBox(height: 30),
        Obx(() =>
            _isAnimating?SizedBox():Text(
          widget.state.isRun==2?("${widget.state.randomNumbers.first}".tr):'',
          style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
          ),
        )),
      ],
    );
  }
}